﻿using FeedsLibrary;
using FeedsLibrary.Feeds.Abstract;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using ThirdPrize.Classes;
using ThirdPrize.Models;

namespace ThirdPrize.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class CodeController : Controller
    {
        private static readonly log4net.ILog WebcomixLog = log4net.LogManager.GetLogger
                                                            ("WebcomiX");
        private static readonly log4net.ILog AppliocationLog = log4net.LogManager.GetLogger
                                                            ("Application");

        private readonly euzycdrv_ThirdPrizeEntities _entities = new euzycdrv_ThirdPrizeEntities();

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            AppliocationLog.Info("Code Accessed");

            return View();
        }

        #region Apps Visitors

        /// <summary>
        /// just log a visit
        /// </summary>
        /// <param name="user">unique user id</param>
        /// <param name="appName">app name</param>
        /// <param name="appVersion">app version</param>
        /// <param name="platform">platform</param>
        [AllowAnonymous]
        public void RegisterVisitV1(string user, string appName, string appVersion, string platform="")
        {
            using (Visit v = new Visit())
            {
                v.UserId = user;
                v.Application = appName;
                v.Version = appVersion;
                v.Platform = platform;

                v.LogVisit();
            }
        }

        /// <summary>
        /// Refresh the RSS feed
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult VisitorTable()
        {
            VisitorTableModel model = new VisitorTableModel();
            using (VisitorTable t = new VisitorTable())
            {
                model.Visits = t.GetResults();

                return View(model);
            }
        }
        #endregion

        #region WebComix bits

        /// <summary>
        /// Register the votes for a user of the WebcomiX app
        /// </summary>
        /// <param name="user"></param>
        /// <param name="votes"></param>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult WebcomiXVote(string user, string votes)
        {
            WebcomixLog.Info(String.Format("WebcomiXVote called - {0}", user));

            ViewBag.User = user;
            ViewBag.Votes = votes;

            try
            {
                using (var v = new Vote())
                {
                    v.UserId = user;
                    v.Votes = votes;
                    ViewBag.Result = v.CastVote() ? "Success" : "Failure";
                }
            }
            catch (Exception e)
            {
                WebcomixLog.Error(GetExceptionString(e));
                ViewBag.Result = "Failure";
            }
            return View();
        }

        /// <summary>
        /// Get the data for the WebcomiX charts screen
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult WebcomiXGetData()
        {
            WebcomixLog.Info("WebcomiXGetData called");

            try
            {
                var results = new VoteShowResults();

                string s = results.GetRawResults();

                ViewBag.Result = s;
            }
            catch (Exception e)
            {
                WebcomixLog.Error(GetExceptionString(e));
            }

            return View();
        }

        #endregion


        private string GetExceptionString(Exception e)
        {
            StringBuilder  s = new StringBuilder(e.Message).Append(Environment.NewLine).AppendLine(e.StackTrace);

            return s.ToString();
        }
    }
}